/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.gameserver.model.L2World;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.model.quest.QuestTimer;
import l2s.gameserver.scripting.ScriptFile;

public class Q227_TestOfTheReformer extends Quest implements ScriptFile
{
	private static final String qn = "Q227_TestOfTheReformer";
	private static final int Pupina = 30118;
	private static final int Sla = 30666;
	private static final int Katari = 30668;
	private static final int OlMahumPilgrimNPC = 30732;
	private static final int Kakan = 30669;
	private static final int Nyakuri = 30670;
	private static final int Ramus = 30667;
	private static final int BookOfReform = 2822;
	private static final int LetterOfIntroduction = 2823;
	private static final int SlasLetter = 2824;
	private static final int Greetings = 2825;
	private static final int OlMahumMoney = 2826;
	private static final int KatarisLetter = 2827;
	private static final int NyakurisLetter = 2828;
	private static final int KakansLetter = 3037;
	private static final int UndeadList = 2829;
	private static final int RamussLetter = 2830;
	private static final int RippedDiary = 2831;
	private static final int HugeNail = 2832;
	private static final int LetterOfBetrayer = 2833;
	private static final int BoneFragment1 = 2834;
	private static final int BoneFragment2 = 2835;
	private static final int BoneFragment3 = 2836;
	private static final int BoneFragment4 = 2837;
	private static final int BoneFragment5 = 2838;
	private static final int MarkOfReformer = 2821;
	private static final int NamelessRevenant = 27099;
	private static final int Aruraune = 27128;
	private static final int OlMahumInspector = 27129;
	private static final int OlMahumBetrayer = 27130;
	private static final int CrimsonWerewolf = 27131;
	private static final int KrudelLizardman = 27132;
	private static final int SilentHorror = 20404;
	private static final int SkeletonLord = 20104;
	private static final int SkeletonMarksman = 20102;
	private static final int MiserySkeleton = 20022;
	private static final int SkeletonArcher = 20100;
	public final int[][] DROPLIST_COND =
	{
		{
			18,
			0,
			SilentHorror,
			0,
			BoneFragment1,
			1,
			70,
			1
		},
		{
			18,
			0,
			SkeletonLord,
			0,
			BoneFragment2,
			1,
			70,
			1
		},
		{
			18,
			0,
			SkeletonMarksman,
			0,
			BoneFragment3,
			1,
			70,
			1
		},
		{
			18,
			0,
			MiserySkeleton,
			0,
			BoneFragment4,
			1,
			70,
			1
		},
		{
			18,
			0,
			SkeletonArcher,
			0,
			BoneFragment5,
			1,
			70,
			1
		}
	};
	private static boolean QuestProf = true;
	
	public Q227_TestOfTheReformer(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Pupina);
		addTalkId(Sla);
		addTalkId(Katari);
		addTalkId(OlMahumPilgrimNPC);
		addTalkId(Kakan);
		addTalkId(Nyakuri);
		addTalkId(Ramus);
		addKillId(NamelessRevenant);
		addKillId(Aruraune);
		addKillId(OlMahumInspector);
		addKillId(OlMahumBetrayer);
		addKillId(CrimsonWerewolf);
		addKillId(KrudelLizardman);
		for (int[] element : DROPLIST_COND)
		{
			addKillId(element[2]);
			questItemIds = new int[]
			{
				element[4]
			};
		}
		questItemIds = new int[]
		{
			BookOfReform,
			HugeNail,
			LetterOfIntroduction,
			SlasLetter,
			KatarisLetter,
			LetterOfBetrayer,
			OlMahumMoney,
			NyakurisLetter,
			UndeadList,
			Greetings,
			KakansLetter,
			RamussLetter,
			RippedDiary
		};
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("30118-04.htm"))
		{
			st.giveItems(BookOfReform, 1);
			st.giveItems(7562, 64);
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("30118-06.htm"))
		{
			st.takeItems(HugeNail, -1);
			st.takeItems(BookOfReform, -1);
			st.giveItems(LetterOfIntroduction, 1);
			st.set("cond", "4");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30666-04.htm"))
		{
			st.takeItems(LetterOfIntroduction, -1);
			st.giveItems(SlasLetter, 1);
			st.set("cond", "5");
			st.setState(Quest.STATE_STARTED);
		}
		else if (event.equalsIgnoreCase("30669-03.htm"))
		{
			if (L2World.getInstance().getNpcById(CrimsonWerewolf) == null)
			{
				st.set("cond", "12");
				st.setState(Quest.STATE_STARTED);
				addSpawn(CrimsonWerewolf, player, false, 0, true);
				startQuestTimer("Wait4", 300000, npc, player, false);
			}
			else
			{
				if (getQuestTimer("Wait4", npc, player) == null)
				{
					startQuestTimer("Wait4", 300000, npc, player, false);
				}
				htmltext = "<html><head><body>Plees wait 5 minutes</body></html>";
			}
		}
		else if (event.equalsIgnoreCase("30670-03.htm"))
		{
			if (L2World.getInstance().getNpcById(KrudelLizardman) == null)
			{
				st.set("cond", "15");
				st.setState(Quest.STATE_STARTED);
				addSpawn(KrudelLizardman, player, false, 0, true);
				startQuestTimer("Wait5", 300000, npc, player, false);
			}
			else
			{
				if (getQuestTimer("Wait5", npc, player) == null)
				{
					startQuestTimer("Wait5", 300000, npc, player, false);
				}
				htmltext = "<html><head><body>Plees wait 5 minutes</body></html>";
			}
		}
		else if (event.equalsIgnoreCase("Wait1"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(Aruraune);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (st.getInt("cond") == 2)
			{
				st.set("cond", "1");
			}
			return null;
		}
		else if (event.equalsIgnoreCase("Wait2"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(OlMahumInspector);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			isQuest = L2World.getInstance().getNpcById(OlMahumPilgrimNPC);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (st.getInt("cond") == 6)
			{
				st.set("cond", "5");
			}
			return null;
		}
		else if (event.equalsIgnoreCase("Wait3"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(OlMahumBetrayer);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			return null;
		}
		else if (event.equalsIgnoreCase("Wait4"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(CrimsonWerewolf);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (st.getInt("cond") == 12)
			{
				st.set("cond", "11");
			}
			return null;
		}
		else if (event.equalsIgnoreCase("Wait5"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(KrudelLizardman);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (st.getInt("cond") == 15)
			{
				st.set("cond", "14");
			}
			return null;
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		int npcId = npc.getNpcId();
		QuestState st = player.getQuestState(qn);
		String htmltext = getNoQuestMsg();
		int cond = st.getInt("cond");
		if (npcId == Pupina)
		{
			if (st.getQuestItemsCount(MarkOfReformer) != 0)
			{
				htmltext = getAlreadyCompletedMsg();
				st.exitQuest(true);
			}
			else if (cond == 0)
			{
				if ((st.getPlayer().getClassId().getId() == 0x0f) || (st.getPlayer().getClassId().getId() == 0x2a))
				{
					if (st.getPlayer().getLevel() >= 39)
					{
						htmltext = "30118-03.htm";
					}
					else
					{
						htmltext = "30118-01.htm";
						st.exitQuest(true);
					}
				}
				else
				{
					htmltext = "30118-02.htm";
					st.exitQuest(true);
				}
			}
			else if (cond == 3)
			{
				htmltext = "30118-05.htm";
			}
			else if (cond >= 4)
			{
				htmltext = "30118-07.htm";
			}
		}
		else if (npcId == Sla)
		{
			if (cond == 4)
			{
				htmltext = "30666-01.htm";
			}
			else if (cond == 5)
			{
				htmltext = "30666-05.htm";
			}
			else if (cond == 10)
			{
				st.takeItems(OlMahumMoney, -1);
				st.giveItems(Greetings, 3);
				htmltext = "30666-06.htm";
				st.set("cond", "11");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 20)
			{
				st.takeItems(KatarisLetter, -1);
				st.takeItems(KakansLetter, -1);
				st.takeItems(NyakurisLetter, -1);
				st.takeItems(RamussLetter, -1);
				st.giveItems(MarkOfReformer, 1);
				st.getPlayer().addExpAndSp(28000, 3600);
				htmltext = "30666-07.htm";
				st.playSound(QuestState.SOUND_FINISH);
				st.exitQuest(true);
			}
		}
		else if (npcId == Katari)
		{
			if ((cond == 5) || (cond == 6))
			{
				L2Npc NPC = L2World.getInstance().getNpcById(OlMahumPilgrimNPC);
				L2Npc Mob = L2World.getInstance().getNpcById(OlMahumInspector);
				if ((NPC == null) && (Mob == null))
				{
					st.takeItems(SlasLetter, -1);
					htmltext = "30668-01.htm";
					st.set("cond", "6");
					st.setState(Quest.STATE_STARTED);
					addSpawn(OlMahumPilgrimNPC, player, false, 0, true);
					addSpawn(OlMahumInspector, player, false, 0, true);
					startQuestTimer("Wait2", 300000, npc, player, false);
				}
				else
				{
					if (getQuestTimer("Wait2", npc, player) == null)
					{
						startQuestTimer("Wait2", 300000, npc, player, false);
					}
					htmltext = "<html><head><body>Plees wait 5 minutes</body></html>";
				}
			}
			else if (cond == 8)
			{
				if (L2World.getInstance().getNpcById(OlMahumBetrayer) == null)
				{
					htmltext = "30668-02.htm";
					addSpawn(OlMahumBetrayer, player, false, 0, true);
					startQuestTimer("Wait3", 300000, npc, player, false);
				}
				else
				{
					if (getQuestTimer("Wait3", npc, player) == null)
					{
						startQuestTimer("Wait3", 300000, npc, player, false);
					}
					htmltext = "<html><head><body>Plees wait 5 minutes</body></html>";
				}
			}
			else if (cond == 9)
			{
				st.takeItems(LetterOfBetrayer, -1);
				st.giveItems(KatarisLetter, 1);
				htmltext = "30668-03.htm";
				st.set("cond", "10");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == OlMahumPilgrimNPC)
		{
			if (cond == 7)
			{
				st.giveItems(OlMahumMoney, 1);
				htmltext = "30732-01.htm";
				st.set("cond", "8");
				st.setState(Quest.STATE_STARTED);
				L2Npc isQuest = L2World.getInstance().getNpcById(OlMahumInspector);
				if (isQuest != null)
				{
					isQuest.deleteMe();
				}
				isQuest = L2World.getInstance().getNpcById(OlMahumPilgrimNPC);
				if (isQuest != null)
				{
					isQuest.deleteMe();
				}
				QuestTimer timer = getQuestTimer("Wait2", npc, player);
				if (timer != null)
				{
					timer.cancel();
				}
			}
		}
		else if (npcId == Kakan)
		{
			if ((cond == 11) || (cond == 12))
			{
				htmltext = "30669-01.htm";
			}
			else if (cond == 13)
			{
				st.takeItems(Greetings, 1);
				st.giveItems(KakansLetter, 1);
				htmltext = "30669-04.htm";
				st.set("cond", "14");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == Nyakuri)
		{
			if ((cond == 14) || (cond == 15))
			{
				htmltext = "30670-01.htm";
			}
			else if (cond == 16)
			{
				st.takeItems(Greetings, 1);
				st.giveItems(NyakurisLetter, 1);
				htmltext = "30670-04.htm";
				st.set("cond", "17");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == Ramus)
		{
			if (cond == 17)
			{
				st.takeItems(Greetings, -1);
				st.giveItems(UndeadList, 1);
				htmltext = "30667-01.htm";
				st.set("cond", "18");
				st.setState(Quest.STATE_STARTED);
			}
			else if (cond == 19)
			{
				st.takeItems(BoneFragment1, -1);
				st.takeItems(BoneFragment2, -1);
				st.takeItems(BoneFragment3, -1);
				st.takeItems(BoneFragment4, -1);
				st.takeItems(BoneFragment5, -1);
				st.takeItems(UndeadList, -1);
				st.giveItems(RamussLetter, 1);
				htmltext = "30667-03.htm";
				st.set("cond", "20");
				st.setState(Quest.STATE_STARTED);
			}
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		int npcId = npc.getNpcId();
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int cond = st.getInt("cond");
		for (int[] element : DROPLIST_COND)
		{
			if ((cond == element[0]) && (npcId == element[2]))
			{
				if ((element[3] == 0) || (st.getQuestItemsCount(element[3]) > 0))
				{
					if (element[5] == 0)
					{
						st.rollAndGive(element[4], element[7], element[6], QuestProf);
					}
					else if (st.rollAndGive(element[4], element[7], element[7], element[5], element[6], QuestProf))
					{
						if ((element[1] != cond) && (element[1] != 0))
						{
							st.set("cond", String.valueOf(element[1]));
							st.setState(Quest.STATE_STARTED);
						}
					}
				}
			}
		}
		if ((cond == 18) && (st.getQuestItemsCount(BoneFragment1) != 0) && (st.getQuestItemsCount(BoneFragment2) != 0) && (st.getQuestItemsCount(BoneFragment3) != 0) && (st.getQuestItemsCount(BoneFragment4) != 0) && (st.getQuestItemsCount(BoneFragment5) != 0))
		{
			st.set("cond", "19");
			st.setState(Quest.STATE_STARTED);
		}
		else if ((npcId == NamelessRevenant) && ((cond == 1) || (cond == 2)))
		{
			if (st.getQuestItemsCount(RippedDiary) < 6)
			{
				st.giveItems(RippedDiary, 1);
			}
			else if (L2World.getInstance().getNpcById(Aruraune) == null)
			{
				st.takeItems(RippedDiary, -1);
				st.set("cond", "2");
				st.setState(Quest.STATE_STARTED);
				addSpawn(Aruraune, player, false, 0, true);
				startQuestTimer("Wait1", 300000, npc, player, false);
			}
			else
			{
				if (getQuestTimer("Wait1", npc, player) == null)
				{
					startQuestTimer("Wait1", 300000, npc, player, false);
				}
			}
		}
		else if (npcId == Aruraune)
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(Aruraune);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			if (cond == 2)
			{
				if (st.getQuestItemsCount(HugeNail) == 0)
				{
					st.giveItems(HugeNail, 1);
				}
				st.set("cond", "3");
				st.setState(Quest.STATE_STARTED);
				QuestTimer timer = getQuestTimer("Wait1", npc, player);
				if (timer != null)
				{
					timer.cancel();
				}
			}
		}
		else if (npcId == OlMahumInspector)
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(OlMahumInspector);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			QuestTimer timer = getQuestTimer("Wait2", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			if (cond == 6)
			{
				st.set("cond", "7");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == OlMahumBetrayer)
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(OlMahumBetrayer);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			QuestTimer timer = getQuestTimer("Wait3", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			if (cond == 8)
			{
				if (st.getQuestItemsCount(LetterOfBetrayer) == 0)
				{
					st.giveItems(LetterOfBetrayer, 1);
				}
				st.set("cond", "9");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == CrimsonWerewolf)
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(CrimsonWerewolf);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			QuestTimer timer = getQuestTimer("Wait4", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			if (cond == 12)
			{
				st.set("cond", "13");
				st.setState(Quest.STATE_STARTED);
			}
		}
		else if (npcId == KrudelLizardman)
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(KrudelLizardman);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			QuestTimer timer = getQuestTimer("Wait5", npc, player);
			if (timer != null)
			{
				timer.cancel();
			}
			if (cond == 15)
			{
				st.set("cond", "16");
				st.setState(Quest.STATE_STARTED);
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q227_TestOfTheReformer(227, qn, "Test Of The Reformer");
	}
}
